package com.orangelife.mobile.property.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.orangelife.mobile.property.adapter.PropertyBillFeeDetailMonthAdapter;
import com.orangelife.mobile.widget.ScrollListView;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 应缴账单详情的年单
 * <p>
 * Created by 吴格非 on 2016-10-20.
 * <p>
 *
 * @author 吴格非
 * @since v3.9.2
 */

public class PropertyBillDetailMonthActivity extends OrangeLifeBaseActivity {

    @Bind(R.id.lvPropertyBillOrderInfo)
    ScrollListView lvPropertyBillOrderInfo;
    @Bind(R.id.tv_title_right)
    TextView tvRight;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tvTip)
    TextView tvTip;

    private PropertyBillFeeDetailMonthAdapter mOrderInfoAdapter;
    private List<Map<String, Object>> yearInfoList;
    private HashMap<String, Object> mOrderInfo;
    private String currentYear = "";
    private boolean canOperate;
    private String mSelectedMonth;
    private int mPosition;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_property_bill_detail_month);
        ButterKnife.bind(this);
        getIntentInfo();
        initData();
        setOrderInfoAdapter();
        initTitle();
        tvTip.setText("缴费月份仅限从欠费开始时间起连月选择，暂不支持跨月选择");
    }

    private void getIntentInfo() {
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            canOperate = bundle.getBoolean("canOperate");
            mPosition = bundle.getInt("position");
            mOrderInfo = (HashMap<String, Object>) bundle.getSerializable("map");
            if (mOrderInfo != null) {
                currentYear = (String) mOrderInfo.get("year");
                mSelectedMonth = (String) mOrderInfo.get("selectedMonth");

            }
        }
    }

    private void initTitle() {
        tvTitle.setText(String.valueOf(currentYear) + "年欠费");
        tvRight.setText("全选");
    }

    private void initData() {
        yearInfoList = (List<Map<String, Object>>) mOrderInfo.get("yearInfo");
    }

    @OnClick({R.id.tv_title_right, R.id.btnConfirm})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_title_right:
                mOrderInfoAdapter.selectAll();
                break;
            case R.id.btnConfirm:
                jumpBackToDetail();
                break;
        }
    }

    private void jumpBackToDetail() {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();

        String selectedMonth = mOrderInfoAdapter.getSelectedMonth();
        String yearSelectedInfo = mOrderInfoAdapter.getYearSelectedInfo();
        String ytotal = mOrderInfoAdapter.getYtotal();
        String yearStatus = mOrderInfoAdapter.getYearStatus();
        LinkedList<String> ids = mOrderInfoAdapter.getFeeDetailIDs();
        bundle.putString("ytotal", ytotal);//总共多少钱
        bundle.putString("selectedMonth", selectedMonth);//总共多少钱
        bundle.putString("yearSelectedInfo", yearSelectedInfo);//总共多少钱
        bundle.putInt("position", mPosition);
        bundle.putString("yearStatus", yearStatus);
        bundle.putSerializable("selectedIds", ids);

        intent.putExtras(bundle);
        setResult(0, intent);
        finish();
    }

    private void setOrderInfoAdapter() {
        mOrderInfoAdapter = new PropertyBillFeeDetailMonthAdapter(this, yearInfoList, tvRight, canOperate, mSelectedMonth);
        lvPropertyBillOrderInfo.setAdapter(mOrderInfoAdapter);
    }


}
